##########################################################################################################################
# version: [v.1.0] date: [Thursday November 17 18:17:51 CST 2022] 
##########################################################################################################################
#
# ChangeLog :
#   2022-11-17 - v1.0
# ------------------------------------------------

COAPTOPDIR = $(CURDIR)
export COAPTOPDIR

-include $(COAPTOPDIR)/config.mk
######################################
# target
######################################
TARGET = coap


#######################################
# paths
#######################################
# Build path
BUILD_DIR = out


#######################################
# compile
#######################################
CC = $(CFG_COMPILER)


######################################
# source
######################################
# C sources
C_SOURCES =  \
	$(wildcard coap/*.c) \
	$(wildcard coap/er-coap-13/*.c) \
	$(wildcard data/*.c) \
	$(wildcard core/*.c) \
	$(wildcard examples/client/*.c) \
	${COAPTOPDIR}/examples/shared/dtlsconnection.c \
	${COAPTOPDIR}/examples/shared/commandline.c \
	${COAPTOPDIR}/examples/shared/memtrace.c \
	${COAPTOPDIR}/examples/shared/platform.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/dtls.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/crypto.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/ccm.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/hmac.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/dtls_debug.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/netq.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/peer.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/dtls_time.c \
	${COAPTOPDIR}/examples/shared/tinydtls/dtls_prng.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/session.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/aes/rijndael.c \
	${COAPTOPDIR}/examples/shared/tinydtls/aes/rijndael_wrap.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/sha2/sha2.c \
    ${COAPTOPDIR}/examples/shared/tinydtls/ecc/ecc.c

# C_SOURCES += $(shell find gui/lvgl/src -type f -name '*.c')


######################################
# C defines
######################################
C_DEFS = \
	-D LWM2M_CLIENT_MODE \
	-D LWM2M_BOOTSTRAP \
	-D LWM2M_SUPPORT_SENML_JSON \
	-D WITH_SHA256 \
	-D WITH_TINYDTLS


######################################
# C includes
######################################
C_INCLUDES =  \
	-I coap \
	-I coap/er-coap-13 \
	-I core \
	-I data \
	-I examples/client \
	-I examples/shared \
	-I include \
	-I ${COAPTOPDIR}/examples/shared/tinydtls \
	-I ${COAPTOPDIR}/examples/shared/tinydtls/aes \
	-I ${COAPTOPDIR}/examples/shared/tinydtls/sha2 \
	-I ${COAPTOPDIR}/examples/shared/tinydtls/ecc 


######################################
# C FLAGS
######################################
CFLAGS += $(C_DEFS) $(C_INCLUDES)
CFLAGS += -DLWM2M_COAP_DEFAULT_BLOCK_SIZE=1024
CFLAGS += -DLWM2M_COAP_HOST=\"$(CFG_HOST_IP)\"
CFLAGS += -DLWM2M_COAP_PORT=%(CFG_HOST_PORT)
CFLAGS += -DLWM2M_COAP_ENDPOINT=$(CFG_ENDPOINT)


#######################################
# LDFLAGS
#######################################
# libraries
# use python3.9
LIBS =
# set python lib path, you should repalce with your python-path
LIBDIR =
LIBFLAGS += $(LIBDIR) $(LIBS)


#######################################
# default action: build all
#######################################
all:  $(BUILD_DIR)/$(TARGET)

# list of objects
# set objects name
OBJECTS = $(addprefix $(BUILD_DIR)/obj/,$(notdir $(C_SOURCES:.c=.o)))
# set find path
vpath %.c $(sort $(dir $(C_SOURCES)))

# build obj
$(BUILD_DIR)/obj/%.o: %.c Makefile | $(BUILD_DIR)/obj 
	$(CC) -c $(CFLAGS)  $< -o $@

# build bin
$(BUILD_DIR)/$(TARGET): $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LIBFLAGS) -o $@

# mkdir
$(BUILD_DIR)/obj:
	mkdir -p $@


#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)

-include $(COAPTOPDIR)/tools/Makefile.kconfig 
# *** EOF ***